package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {

    //1.创建sqlsessionfactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Brand> SelectAll() {
        //2.获取sqlsession 对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用方法
        List<Brand> brands = mapper.selectAll();
        //释放资源
        sqlSession.close();
        return brands;
    }

    @Override
    public void add(Brand brand) {
        //2.获取sqlsession 对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用方法
        mapper.add(brand);
        sqlSession.commit();//提交事务
        //释放资源
        sqlSession.close();

    }

    @Override
    public Brand selectById(int id) {
        //2.获取sqlsession 对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用方法
        Brand brand = mapper.selectById(id);
        //释放资源
        sqlSession.close();
        return brand;
    }

    @Override
    public void deleteByIds(int[] ids) {
        //2.获取sqlsession 对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用方法
        mapper.deleteByIds(ids);
        sqlSession.commit();//提交事务
        //释放资源
        sqlSession.close();
    }

    @Override
    public void deleteById(int id) {
        //2.获取sqlsession 对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用方法
        mapper.deleteById(id);
        sqlSession.commit();//提交事务
        //释放资源
        sqlSession.close();
    }

    @Override
    public PageBean<Brand> seletByPage(int currentPage, int pageSize) {
        //2.获取sqlsession 对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用方法
        int begin =(currentPage - 1) * pageSize;
        int size  = pageSize;

        List<Brand> rows = mapper.selectByPage(begin,size);
        int totalCount = mapper.selectTotalCount();
        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        //释放资源
        sqlSession.close();
        return pageBean;
    }

    @Override
    public PageBean<Brand> seletByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //2.获取sqlsession 对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用方法
        int begin =(currentPage - 1) * pageSize;
        int size  = pageSize;

        //处理brand条件  模糊表达式
        if(brand.getBrandName() != null && brand.getBrandName().length() > 0){
            brand.setBrandName("%" + brand.getBrandName() + "%");
        }
        if(brand.getCompanyName() != null && brand.getCompanyName().length() > 0){
            brand.setCompanyName("%" + brand.getCompanyName() + "%");
        }
        List<Brand> rows = mapper.selectByPageAndCondition(begin,size,brand);
        int totalCount = mapper.selectTotalCountByConditon(brand);
        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        //释放资源
        sqlSession.close();
        return pageBean;
    }

    @Override
    public void update(Brand brand) {
        //2.获取sqlsession 对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4.调用方法
        mapper.update(brand);
        sqlSession.commit();//提交事务
        //释放资源
        sqlSession.close();
    }
}
